/*
* @Author: majingyu
* @Date:   2017-05-08 15:05:46
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-06 13:04:08
*/

import React, { Component }from 'react';
import { connect } from 'react-redux';
import RequireList from 'components/requireList.jsx';
import SkillList from 'components/skillList.jsx';
import { eidtServiceTab, eidtServiceInfoAction, eidtSkillInfoAction, addTips, invitesDemandOrder,invitesSkillOrder} from 'action/service.js';
import 'css/components/tabs.less';
import { local } from 'common/local.js';
import { changeCommentId } from 'action/commont.js';
import { getPageList } from 'common/pagingDevice';

class NearByList extends Component {
    constructor(props) {
        super(props);
        let params = local();
        this.state = {
            lat: params.lat,
            lng: params.lng
        }
        this.getDemandList();
    } 
    getScollDemand(){
        changeCommentId(this.getSkillList())
    } 
    changeActive(tabName){
        this.props.changeActive(tabName);
        if(tabName == 1){
            this.getDemandList();
        }else{
            this.getSkillList();
        }
    }
    getMoreList(){
        if(this.props.services.activeKey == 1){
            this.getDemandList(this.props.services.nearListList.pageNo+1);
        }else{
            this.getSkillList(this.props.services.nearSkillList.pageNo+1);
        }
        
    }
    getDemandList(page){
        let data= {
            pageNumber: page || this.props.services.nearListList.pageNo,
            pageSize: 10,
            distance: 15,
            lat: this.state.lat,
            lng: this.state.lng
        }
        this.props.changeDemandList(data);
    } 
    getSkillList(page){
        let data= {
            pageNumber: page || this.props.services.nearSkillList.pageNo,
            pageSize: 10,
            distance: 15,
            lat: this.state.lat,
            lng: this.state.lng
        }
        this.props.changeSkillList(data);
    }
    commontList(id){
        this.props.changeCommentId(id);
        window.location.href = '#/DemandDetail';
    }
    goSkillList(id){
        this.props.changeCommentId(id);
        window.location.href = '#/skillDetail'
    }
    clickTips(id){
        let params = {
            id: id
        }
        this.props.dispatch(addTips(params));
    }
    inviteDemandOrder(id){
        let param = {
            id: id
        }
        this.props.inviteDemandOrder(param);
    }
    inviteSkillOrder(id){
        let param = {
            id: id
        }
        this.props.inviteSkillOrder(param);
    }
    render(){
        return (
            <div className="content">
                <ul className="subNavWrap">
                    <li key='servicesList' onClick={()=>{this.changeActive(1)}} className={this.props.services.activeKey  == '1'? 'active':'tabList'}>周边服务</li>
                    <li key='mySkillList' onClick={()=>{this.changeActive(2)}} className={this.props.services.activeKey == '2' ? 'active':'tabList'}>周边需求</li>
                </ul>
                {
                    this.props.services.activeKey == 1 ? 
                    <RequireList servicesLists = {this.props.services.nearListList} commontList={(id) => {this.commontList(id)}} clickTips={(id) => {this.clickTips(id)}} inviteOrder={(id)=>this.inviteDemandOrder(id)} onScoll={() => {this.getScollDemand()}} getMoreList={() => {this.getMoreList()}}/>
                    :<SkillList skillList = {this.props.services.nearSkillList} goSkillList={(id) => {this.goSkillList(id)}} clickTips={(id) => {this.clickTips(id)}} inviteOrder={(id)=>{this.inviteSkillOrder(id)}} goScoll={() => {this.getScollDemand()}} getMoreList={() => {this.getMoreList()}}/>
                }
                
            </div>
        );
    }
}

const NearByData = (state) => {
    return {
        app:state.app,
        services: state.services
    }
}
let dispatchHandle = (dispatch, getState) => {
    return {
        changeActive(tabName){
            dispatch(eidtServiceTab(tabName));
        },
        changeDemandList(data){
            dispatch(eidtServiceInfoAction(data));
        },
        changeSkillList(data){
             dispatch(eidtSkillInfoAction(data));
        },
        changeCommentId(id){
            dispatch(changeCommentId(id));
        },
        inviteDemandOrder(data){
            dispatch(invitesDemandOrder(data))
        },
        inviteSkillOrder(data){
            dispatch(invitesSkillOrder(data))
        }
    }
    
}
let visibileNearBy= connect(NearByData, dispatchHandle)(NearByList);


export default visibileNearBy;